#ifndef __APP_DEVICE_H__
#define __APP_DEVICE_H__
#include "app/app_buffer.h"
#include "app/app_task.h"
#include "app/app_message.h"
#include <sys/types.h>

struct VTable;

typedef struct DeviceStruct
{
    struct VTable *vptr;  // 设备函数虚表
    char *filename;
    int fd;
    pthread_t  background_thread; // 设备同步线程
    ConnectionType connection_type; // 连接类型
    Buffer *recv_buffer;
    Buffer *send_buffer;
    int is_running;
} Device;

struct VTable
{
    void* (*background_task)(void *);
    Task recv_task;
    Task send_task;
    int (*post_read)(Device *device, void *ptr, int *len); // 读后处理函数
    int (*pre_write)(Device *device, void *ptr, int *len); // 写前处理函数
    int (*recv_callback)(void *ptr, int len);

};

int app_device_init(Device *device, char *filename);

// 启动设备后台线程
int app_device_start(Device *device);

// 停止设备后台线程
void app_device_stop(Device *device);

int app_device_write(Device *device, void *ptr, int len);

// 这里需要一个注册回调函数方法

void app_device_registerRecvCallback(Device *device, int (*recv_callback)(void *,int));
void app_device_close(Device *device);


#endif 
